package com.supersoul.myself.service.impl;

import com.supersoul.myself.entity.bo.Prize;
import com.supersoul.myself.entity.bo.PrizeBean;
import com.supersoul.myself.enums.PrizeTypeEnum;
import com.supersoul.myself.ex.ServiceException;
import com.supersoul.myself.service.LotteryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


@Service
@Slf4j
public class LotteryServiceImpl implements LotteryService {

    public static List<Prize> prizeList = new ArrayList<>();

    static {
        log.info("==========================初始化奖品开始==============================");
        Prize prize1 = new Prize("1", "亲吻老宽", PrizeTypeEnum.Score.getPrizeType(), 100, 30 );
        Prize prize2 = new Prize("2", "亲吻胖哥", PrizeTypeEnum.Coupon.getPrizeType(), 200, 30 );
        Prize prize3 = new Prize("3", "亲吻波多野结衣老师", PrizeTypeEnum.Thanks.getPrizeType(), 0, 30 );
        Prize prize4 = new Prize("4", "你是一个好人", PrizeTypeEnum.Thanks.getPrizeType(), 0, 10 );
        prizeList.add(prize1);
        prizeList.add(prize2);
        prizeList.add(prize3);
        prizeList.add(prize4);
        log.info("prizeList={}", prizeList);
        log.info("==========================初始化奖品结束==============================");
    }


    @Override
    public Prize goLottery() throws ServiceException {
        log.info("==========================抽奖开始==============================");
        List<PrizeBean> prizePool = generatePrizePool(prizeList);
        if (prizePool == null) {
            log.info("无法生成奖池,返回奖品prize=null");
            return null;
        }
        Prize prize = getPrize(prizePool);
        // 异步发奖
        new Thread(() -> {
            try {
                sendPrize(prize);
            } catch (ServiceException e) {
                e.printStackTrace();
            }
        }).start();
        log.info("==========================抽奖结束==============================");
        log.info("所中奖品prize={}", prize);
        return prize;
    }

    @Override
    public List<PrizeBean> generatePrizePool(List<Prize> prizeList) throws ServiceException {
        if (!CollectionUtils.isEmpty(prizeList)) {
            log.info("==========================生成奖池开始==============================");
            // 定义总值.
            int total = 100;
            int temp = 0;
            List<PrizeBean> prizePool = new ArrayList<>();
            // 奖池设置
            for (Prize prize : prizeList) {
                PrizeBean prizeBean = new PrizeBean();
                prizeBean.setPrize(prize);
                prizeBean.setStart(temp);
                temp = temp + (prize.getPrizeProbability()*total)/100;
                prizeBean.setEnd(temp);
                prizePool.add(prizeBean);
            }
            log.info("生成奖池信息prizePool={}", prizePool);
            log.info("==========================生成奖池结束==============================");
            return prizePool;
        }
        return null;
    }

    @Override
    public Prize getPrize(List<PrizeBean> prizePool) throws ServiceException {
        log.info("==========================开始摇奖==============================");
        // 来一个随机数, 看落在哪个区间
        Random random = new Random();
        int randomNum = random.nextInt(100);
        log.info("随机数randomNum={}", randomNum);
        for (PrizeBean prizeBean : prizePool) {
            if (randomNum > prizeBean.getStart() && randomNum < prizeBean.getEnd()) {
                log.info("获取奖品prize={}, 区间{}-{}", prizeBean.getPrize(), prizeBean.getStart(), prizeBean.getEnd());
                log.info("==========================摇奖结束==============================");
                return prizeBean.getPrize();
            }
        }
        throw new ServiceException("摇奖有问题, 根本没有落在区间上");
    }

    @Override
    public void sendPrize(Prize prize) throws ServiceException {

    }


}
